<?php

namespace app\selladmin\controller;

use app\selladmin\controller\CommonController;
use app\selladmin\model\Item;
use app\selladmin\model\Order;
use app\selladmin\model\Sms;
use app\selladmin\model\Staff;
use app\selladmin\model\Store;
use app\selladmin\model\Custom;
use app\selladmin\model\Withdraw;
use app\v300\model\Code;
use app\selladmin\model\Sellorder;
use app\selladmin\model\Selladmin;

class IndexController extends CommonController {
    /*
     * 基本信息
     * author xfwed 
     * date 2017.08.02
     */

    public function index() {
        //店铺总数
        $store = new Store();
        $storeNum = $store->where('is_del', '=', 1)->where('auditstore', '=', 1)->where('sell_id', '<>', 0)->count();
        //总收费
        $sellmoney = Sellorder::where('status', '=', 2)->sum('pay_money');
        //待审核
        $AuitStoreNum = $store->where("(auditstore=0 or auditstore=2) and is_del=1 and sell_id!=0")->count();
        //待分配
        $assignStoreNum = $store->where("auditstore=1 and is_del=1 and sell_id=0 and add_time>1546272000")->count();
        //个人信息
        $sellinfo = Selladmin::where('id', '=', session('S_ID'))->find();
        $ordernum = Sellorder::where('sell_id', '=', session('S_ID'))->where('status', '=', 2)->count();
        $moneynum = Sellorder::where('sell_id', '=', session('S_ID'))->where('status', '=', 2)->sum('pay_money');
        $sellinfo['ordernum'] = $ordernum;
        $sellinfo['moneynum'] = $moneynum;

        //排行榜
        $selladmin = Selladmin::where('level', '=', 2)->where('status', '=', 1)->select();
        $topdata = [];
        for ($i = 0; $i < count($selladmin); $i++) {
            $sellids = Selladmin::where('pid', '=', $selladmin[$i]['id'])->where('status', '=', 1)->column('id');
            $topdata[$i]['name'] = $selladmin[$i]['admin_note'] . '团队';
            $topdata[$i]['num'] = count($sellids);
            $topdata[$i]['money'] = Sellorder::where('sell_id', 'in', $sellids)->sum('pay_money');
        }
        array_multisort(array_column($topdata,'money'),SORT_DESC,$topdata);
        
        //销售总数
        $sellnum = Selladmin::where('status','=', 1)->where('level','=',3)->count();
        
        //收费比重
        $money1 = Sellorder::where('type','=',1)->sum('pay_money');
        $money2 = Sellorder::where('type','=',2)->sum('pay_money');
        $money3 = Sellorder::where('type','=',3)->sum('pay_money');
        $money4 = Sellorder::where('type','=',4)->sum('pay_money');
        $money['type1'] = isset($money1)?$money1:0;
        $money['type2'] = isset($money2)?$money2:0;
        $money['type3'] = isset($money3)?$money3:0;
        $money['type4'] = isset($money4)?$money4:0;
        
        //个人排行榜
        $sellassign = Selladmin::where('level','=',3)
                ->alias('s')
                ->where('status','=',1)
                ->field('s.id,s.pid,s.admin_note,s.photo,(SELECT sum(pay_money) FROM da_sellorder WHERE sell_id=s.id) as money')
                ->order('money desc')
                ->limit(0,8)
                ->select();
        for($i=0;$i<count($sellassign);$i++){
            $pname = Selladmin::where('id','=',$sellassign[$i]['pid'])->field('admin_note')->find();
            $sellassign[$i]['pname'] = $pname['admin_note'];
        }
        

        return view('index', [
            'storeNum' => $storeNum,
            'sellmoney' => $sellmoney,
            'AuitStoreNum' => $AuitStoreNum,
            'assignStoreNum' => $assignStoreNum,
            'sellinfo' => $sellinfo,
            'topdata' => $topdata,
            'sellnum' => $sellnum,
            'money' => $money,
            'sellassign' => $sellassign,
        ]);
    }

    /*
     * 店铺展示
     * author xfwed 
     * date 2017.08.02
     */

    public function shopmap() {
        $year = isset($_REQUEST['year']) ? $_REQUEST['year'] : date('Y');
        //月份
        for ($m = 1; $m <= 12; $m++) {
            $month[] = $m . "月";
        }
        $store = new Store();
        //店铺量
        $data = $store
                ->field("FROM_UNIXTIME(add_time, '%m') AS m,count(id) AS c")
                ->where("FROM_UNIXTIME(add_time,'%Y') = '" . $year . "' and auditstore = 1")
                ->group('m')
                ->order('m asc')
                ->select();
        $return_data = array(
            '01' => 0, '02' => 0, '03' => 0, '04' => 0, '05' => 0, '06' => 0, '07' => 0, '08' => 0, '09' => 0, '10' => 0, '11' => 0, '12' => 0,
        );
        foreach ($data as $value) {
            $return_data[$value['m']] = $value['c'];
        }

        //用户量
        $customData = Custom::field("FROM_UNIXTIME(add_time, '%m') AS m,count(id) AS c")
                ->where("FROM_UNIXTIME(add_time,'%Y') = '" . $year . "' and status=2")
                ->group('m')
                ->order('m asc')
                ->select();
        $cusReturn_data = array(
            '01' => 0, '02' => 0, '03' => 0, '04' => 0, '05' => 0, '06' => 0, '07' => 0, '08' => 0, '09' => 0, '10' => 0, '11' => 0, '12' => 0,
        );
        foreach ($customData as $value) {
            $cusReturn_data[$value['m']] = $value['c'];
        }

        //收入
        $orderwhere["FROM_UNIXTIME(add_time,'%Y')"] = $year;
        $orderwhere["status"] = ['>', 1];
        $orderwhere["ordtype"] = ['in', [1, 3, 4, 5, 6, 7]];

        $orderData = Order::field("FROM_UNIXTIME(add_time, '%m') AS m,sum(pay_money) AS p")
                ->where($orderwhere)
                ->group('m')
                ->order('m asc')
                ->select();
        $orderReturn_data = array(
            '01' => 0, '02' => 0, '03' => 0, '04' => 0, '05' => 0, '06' => 0, '07' => 0, '08' => 0, '09' => 0, '10' => 0, '11' => 0, '12' => 0,
        );
        foreach ($orderData as $value) {
            $orderReturn_data[$value['m']] = $value['p'];
        }

        //支出
        $moneyData = Withdraw::field("FROM_UNIXTIME(utime, '%m') AS u,sum(money) AS m,sum(poundage) AS p")
                ->where("FROM_UNIXTIME(utime,'%Y') = '" . $year . "' and status=2")
                ->group('u')
                ->order('u asc')
                ->select();
        $moneyReturn_data = array(
            '01' => 0, '02' => 0, '03' => 0, '04' => 0, '05' => 0, '06' => 0, '07' => 0, '08' => 0, '09' => 0, '10' => 0, '11' => 0, '12' => 0,
        );
        foreach ($moneyData as $value) {
            $moneyReturn_data[$value['u']] = $value['m'] + $value['p'];
        }

        //支付方式
        //$paytypewhere["FROM_UNIXTIME(add_time,'%Y')"] = $year;
        $paytypewhere["status"] = ['>', 1];
        $paytypewhere["ordtype"] = ['in', [1, 2, 3, 4, 5, 6, 7]];

        $paytypeData = Order::field("ordtype AS o,sum(pay_money) AS p")
                ->where($paytypewhere)
                ->group('o')
                ->select();
        $paytypeReturn_data = array(
            '1' => 0, '2' => 0, '3' => 0, '4' => 0, '5' => 0, '6' => 0, '7' => 0
        );
        foreach ($paytypeData as $value) {
            $paytypeReturn_data[$value['o']] = $value['p'];
        }

        //不同类型的订单金额
        $itemapp[0] = Order::where('type', '=', '2')->where('ordtype', 'in', '3,4')->where('status', '>', 1)->sum('pay_money');
        $itemapp[1] = Order::where('type', '=', '2')->where('ordtype', 'in', '5,6')->where('status', '>', 1)->sum('pay_money');
        $itemapp[2] = Order::where('type', '=', '2')->where('ordtype', 'in', '7')->where('status', '>', 1)->sum('pay_money');
        $itemapp[3] = Order::where('type', '=', '2')->where('ordtype', 'in', '2')->where('status', '>', 1)->sum('pay_money');

        $miaoshaapp[0] = Order::where('type', '=', '5')->where('ordtype', 'in', '3,4')->where('status', '>', 1)->sum('pay_money');
        $miaoshaapp[1] = Order::where('type', '=', '5')->where('ordtype', 'in', '5,6')->where('status', '>', 1)->sum('pay_money');
        $miaoshaapp[2] = Order::where('type', '=', '5')->where('ordtype', 'in', '7')->where('status', '>', 1)->sum('pay_money');
        $miaoshaapp[3] = Order::where('type', '=', '5')->where('ordtype', 'in', '2')->where('status', '>', 1)->sum('pay_money');

        $tiyankaapp[0] = Order::where('type', '=', '4')->where('ordtype', 'in', '3,4')->where('status', '>', 1)->sum('pay_money');
        $tiyankaapp[1] = Order::where('type', '=', '4')->where('ordtype', 'in', '5,6')->where('status', '>', 1)->sum('pay_money');
        $tiyankaapp[2] = Order::where('type', '=', '4')->where('ordtype', 'in', '7')->where('status', '>', 1)->sum('pay_money');
        $tiyankaapp[3] = Order::where('type', '=', '4')->where('ordtype', 'in', '2')->where('status', '>', 1)->sum('pay_money');

        $vipapp[0] = Order::where('type', '=', '3')->where('ordtype', 'in', '3,4')->where('status', '>', 1)->sum('pay_money');
        $vipapp[1] = Order::where('type', '=', '3')->where('ordtype', 'in', '5,6')->where('status', '>', 1)->sum('pay_money');
        $vipapp[2] = Order::where('type', '=', '3')->where('ordtype', 'in', '7')->where('status', '>', 1)->sum('pay_money');
        $vipapp[3] = Order::where('type', '=', '3')->where('ordtype', 'in', '2')->where('status', '>', 1)->sum('pay_money');
        //print_r($paytypeReturn_data);die;

        return view('shopmap', [
            'storedata' => $return_data,
            'customdata' => $cusReturn_data,
            'order' => $orderReturn_data,
            'money' => $moneyReturn_data,
            'paytype' => $paytypeReturn_data,
            'itemapp' => $itemapp,
            'miaoshaapp' => $miaoshaapp,
            'tiyankaapp' => $tiyankaapp,
            'vipapp' => $vipapp
        ]);
    }

    /*
     * 退出登录
     * author xfwed
     * date 2017.08.02
     */

    public function logout() {
        session_unset();
        session_destroy();
        $this->redirect('selladmin/login/index');
    }

}
